<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category TagService.php
 * @Depiction:
 * Author: hwy
 * Date: 2021/2/2  15:31
 * +--------------------------------------------------------+
 */

namespace App\Services;

use App\Exceptions\ResponseException;
use App\Models\Tag;
use Illuminate\Support\Facades\DB;

class TagService extends BaseService
{
    /**
     * @var Tag
     */
    protected $tag;

    /**
     * TagService constructor.
     */
    public function __construct()
    {
        $this->tag = new Tag();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:41
     */
    public function getAll()
    {
        return $this->tag->query()->get();
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:41
     */
    public function getOne($params)
    {
        return $this->tag->find($params);
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:41
     */
    public function getList($params)
    {
        $query   = $this->tag->query();
        $perPage = $this->perPage($params);
        $query   = $this->paramsSet($params, $query);
        $list    = $query->orderBy('order', 'asc')->orderBy('id', 'desc')->paginate($perPage)->toArray();

        return return_success_msg(ResponseException::REQUEST_SUCCESS_MESSAGE, $list);
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:41
     */
    public function addData($params)
    {
        $add_data = $params->input();

        try {
            $add = $this->tag->create($add_data);
        } catch (\Exception $e) {
            return return_error_msg($e->getMessage());
        }

        if ($add) {
            return return_success_msg(ResponseException::SUCCESS_ADD_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_ADD_MESSAGE);
        }
    }

    /**
     * @param $params
     * @param $id
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:41
     */
    public function editData($params, $id)
    {
        $data = $this->getOne($id);
        if (empty($data)) {
            return return_error_msg(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }
        $edit_data = $params->input();

        try {
            $edit = $data->update($edit_data);
        } catch (\Exception $e) {
            return return_error_msg($e->getMessage());
        }
        if ($edit) {
            return return_success_msg(ResponseException::SUCCESS_EDIT_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_EDIT_MESSAGE);
        }

    }

    /**
     * @param $ids
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:42
     */
    public function delData($ids)
    {
        if(is_string($ids)){
            $ids = [$ids];
        }
        $data = $this->tag->query()->whereIn('id',$ids)->get();
        if (empty($data)) {
            return return_error_msg(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }
        DB::beginTransaction();
        foreach ($data as $value){
            try {
                $del = $value->delete();
            } catch (\Exception $e) {
                DB::rollBack();
                return return_error_msg($e->getMessage());
            }
        }

        if ($del) {
            DB::commit();
            return return_success_msg(ResponseException::SUCCESS_DELETE_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_DELETE_MESSAGE);
        }

    }

}
